<?php

namespace App\Jobs;

use App\Internals\StudyInternal;

class WeeklyStatJob extends Job
{
    protected $userId;
    protected $wordCount;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($userId, $wordCount)
    {
        $this->userId = $userId;
        $this->wordCount = $wordCount;
    }

    public function handle()
    {
        // 获取开始学习时间
        $studyTime = StudyInternal::getStartTimeLearn($this->userId);

        $startTime = $studyTime['startTime'] ? strtotime($studyTime['startTime']) : time();
        $week = $studyTime['week'];

        $startDate = date('Y-m-d', strtotime('+ '.($week-1).' week', $startTime));
        $endDate = date('Y-m-d', strtotime('+ '.$week.' week', $startTime));

        StudyInternal::weeklyStatStore([
            'userId' => $this->userId,
            'week' => $week,
            'incrWordCount' => $this->wordCount,
            'startDate' => $startDate,
            'endDate' => $endDate,
        ]);
    }

}
